package com.ruoyi.project.newhope.inspection.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.newhope.inspection.domain.NewHopeInspection;
import com.ruoyi.project.newhope.inspection.service.INewHopeInspectionService;
import com.ruoyi.project.newhope.materialInspection.domain.NewHopeMaterialInspection;
import com.ruoyi.project.newhope.materialInspection.service.INewHopeMaterialInspectionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 化验项目设置Controller
 * 
 * @author ruoyi
 * @date 2021-02-08
 */
@Controller
@RequestMapping("/api/newhope/inspection")
public class NewHopeInspectionController extends BaseController
{

    @Autowired
    private INewHopeInspectionService newHopeInspectionService;
    @Autowired
    private INewHopeMaterialInspectionService newHopeMaterialInspectionService;

    /**
     * 查询化验项目设置列表
     */
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(NewHopeInspection newHopeInspection)
    {
        startPage();
        List<NewHopeInspection> list = newHopeInspectionService.selectNewHopeInspectionList(newHopeInspection);
        return getDataTable(list);
    }

    /**
     * 查询化验项目设置列表
     */
    @GetMapping("/all")
    @ResponseBody
    public AjaxResult getAll(NewHopeInspection newHopeInspection)
    {
        List<NewHopeInspection> list = newHopeInspectionService.selectNewHopeInspectionList(newHopeInspection);
        return AjaxResult.success(list);
    }

    /**
     * 导出化验项目设置列表
     */
    @Log(title = "化验项目设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(@RequestBody NewHopeInspection newHopeInspection)
    {
        List<NewHopeInspection> list = newHopeInspectionService.selectNewHopeInspectionList(newHopeInspection);
        ExcelUtil<NewHopeInspection> util = new ExcelUtil<NewHopeInspection>(NewHopeInspection.class);
        return util.exportExcel(list, "inspection");
    }

    /**
     * 新增保存化验项目设置
     */
    @Log(title = "化验项目设置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody NewHopeInspection newHopeInspection)
    {
        if (newHopeInspection.getInspectionCode() == null) {
            return AjaxResult.error("化验编码不能为空");
        }
        if (newHopeInspection.getInspectionDesc() == null) {
            return AjaxResult.error("化验名称不能为空");
        }
        Integer checkFlag = newHopeInspectionService.isExist(newHopeInspection);
        if (checkFlag > 0) {
            return AjaxResult.error("化验编码，化验名称 和其他项冲突存在重复!");
        }
        return toAjax(newHopeInspectionService.insertNewHopeInspection(newHopeInspection));
    }

    /**
     * 修改化验项目设置
     */
    @GetMapping("/edit/{id}")
    @ResponseBody
    public NewHopeInspection edit(@PathVariable("id") Long id)
    {
        NewHopeInspection newHopeInspection = newHopeInspectionService.selectNewHopeInspectionById(id);
        return newHopeInspection;
    }

    /**
     * 修改保存化验项目设置
     */
    @Log(title = "化验项目设置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody NewHopeInspection newHopeInspection)
    {
        if (newHopeInspection.getInspectionCode() == null) {
            return AjaxResult.error("化验编码不能为空");
        }
        if (newHopeInspection.getInspectionDesc() == null) {
            return AjaxResult.error("化验名称不能为空");
        }
        Integer checkFlag = newHopeInspectionService.isExist(newHopeInspection);
        if (checkFlag > 1) {
            return AjaxResult.error("化验编码，化验名称 和其他项冲突存在重复!");
        }
        return toAjax(newHopeInspectionService.updateNewHopeInspection(newHopeInspection));
    }

    /**
     * 删除化验项目设置
     */
    @Log(title = "化验项目设置", businessType = BusinessType.DELETE)
    @GetMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String id)
    {
        if (StringUtils.isEmpty(id)) {
            return AjaxResult.error("删除化验项目不能为空");
        }
        //判断当前化验项目是否存在于关联表中
        NewHopeMaterialInspection newHopeMaterialInspection = new NewHopeMaterialInspection();
        newHopeMaterialInspection.setInspectionId(Long.parseLong(id));
        List<NewHopeMaterialInspection> newHopeMaterialInspections = newHopeMaterialInspectionService.selectNewHopeMaterialInspectionList(newHopeMaterialInspection);
        if (newHopeMaterialInspections != null && newHopeMaterialInspections.size() > 0) {
            return AjaxResult.error("当前化验项目在 物料检测项目设置 正处于引用状态，请先删除 物料检测项目设置 中的引用");
        }
        return toAjax(newHopeInspectionService.deleteNewHopeInspectionByIds(id));
    }
}
